package com.example.knowledgeget1.KnowtController;

import com.example.knowledgeget1.Common.HttpStatusEnum;
import com.example.knowledgeget1.Common.ResponseResult;
import com.example.knowledgeget1.KnowtCommon.Entity.Teacher;
import com.example.knowledgeget1.KnowtCommon.Request.StudentLoginRequest;
import com.example.knowledgeget1.KnowtCommon.Request.TeacherLoginRequest;
import com.example.knowledgeget1.KnowtCommon.Response.StudentLoginResponse;
import com.example.knowledgeget1.KnowtCommon.Service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/teachers")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    @PostMapping("/")
    public Teacher createTeacher(@RequestBody Teacher teacher) {
        return teacherService.createTeacher(teacher);
    }

    @GetMapping("/")
    public List<Teacher> getAllTeachers() {
        return teacherService.getAllTeachers();
    }

    @GetMapping("/{id}")
    public Teacher getTeacherById(@PathVariable Long id) {
        return teacherService.getTeacherById(id);
    }

    @PutMapping("/{id}")
    public Teacher updateTeacher(@PathVariable Long id, @RequestBody Teacher teacher) {
        return teacherService.updateTeacher(id, teacher);
    }

    @DeleteMapping("/{id}")
    public void deleteTeacher(@PathVariable Long id) {
        teacherService.deleteTeacher(id);
    }

    @PostMapping("/login")
    public ResponseResult teacherLoginAndGetToken(@RequestBody TeacherLoginRequest loginRequest) {
        Map<String, String> response = teacherService.teacherLoginAndGetToken(loginRequest.getTeaId(), loginRequest.getTeaPwd());
        if (response != null) {
            return ResponseResult.success(response); // 验证成功，返回包含账号和token的JSON响应
        }
        return ResponseResult.fail(HttpStatusEnum.UNAUTHORIZED); // 验证失败
    }
}